home *** CD-ROM | disk | FTP | other *** search
/ Merciful 2 / Merciful - Disc 2.iso / software / d / devioustools26.dms / devioustools26.adf / utils / 001.lzx / AmiPhone / EditTextFile.rexx < prev    next >
OS/2 REXX Batch file  |  1994-05-16  |  3KB  |  110 lines

  1. /* 
  2.    EditTextFile.rexx v1.0 by Jeremy Friesner
  3.  
  4.    An ARexx script to cleanly modify text config
  5.    lines.  Any line beginning with sReplaceMe in 
  6.    the File fModifyMe will be replaced with the line sWithMe.
  7.    
  8.    If no such thing as sReplaceMe is found in the
  9.    file, then sWithMe will be added to the end of
  10.    the file. 
  11.  
  12.    This script should never lose the original file:
  13.    the first thing it does it rename the file to
  14.    filename.bak and then reconstructs the modified
  15.    version at the original file name by reading the
  16.    .bak file.   
  17.    
  18.    Any ^ signs in the sReplaceMe or sWithMe args will
  19.    be turned into space characters.  (Although sReplaceMe
  20.    doesn't actually need these, because it's the last
  21.    arg and thus spaces will be included in it anyway.
  22.    Isn't Rexx argument passing fun?)
  23. */
  24.    
  25. parse arg fModifyMe sReplaceMe sWithMe
  26.  
  27. address COMMAND
  28.  
  29. sReplaceMe = ChangeToSpaces(sReplaceMe)
  30. sWithMe    = strip(ChangeToSpaces(sWithMe))
  31.  
  32. fBackupFile = fModifyMe || ".bak"
  33. say "File to be modified = [" || fModifyMe || "]"
  34. say "Backup file         = [" || fBackupFile || "]"
  35. say "String to replace   = [" || sReplaceMe || "]"
  36. say "with                = [" || sWithMe || "]"
  37.  
  38. keylength = length(sReplaceMe)
  39.  
  40. /* First, make sure rexxsupport.library is available. */
  41. call addlib("rexxsupport.library", 0, -30, 0)
  42.  
  43. /* First, rename the original file to filename.bak */
  44.  
  45. /* Delete the .bak file if it exists */
  46. call delete(fBackupFile)
  47. if (rename(fModifyMe, fBackupFile) == 0) then do
  48.     say "Error:  Couldn't rename " || fModifyMe || " to " || fBackupFile
  49.     exit(30)
  50.     end
  51.     
  52. /* Default == haven't found the line we want to replace yet */
  53. BFoundOurLine = 0
  54.  
  55. /* Open the backup file for reading */
  56. if (open('oldfile',fBackupFile,'R') == 0) then do
  57.     say "Couldn't open backup file " || fBackupFile
  58.     exit(31)
  59.     end
  60.     
  61. /* and the new file for writing */
  62. if (open('newfile',fModifyMe,'W') == 0) then do
  63.     say "Couldn't open output file " || fModifyMe
  64.     exit(32)
  65.     end
  66.  
  67. /* Scan the old file, writing the new one */
  68. do while (EOF('oldfile') == 0)
  69.  nextline = readln('oldfile')
  70.  sCheckPart = left(nextline,keylength)
  71.  
  72.  if ((BFoundOurLine == 0)&(sCheckPart = sReplaceMe)) then 
  73.  do
  74.      /* don't write out this line... write out our substitute instead! */
  75.      say "Found: [" || nextline || "] replacing with [" || sWithMe || "]"
  76.     call writeln('newfile',sWithMe)
  77.     BFoundOurLine = 1
  78.  end
  79.  else do
  80.     call writeln('newfile',nextline)
  81.     end
  82. end
  83.  
  84. /* If we never found our line to replace, then just add our
  85.    line at the end of the file. */
  86. if (BFoundOurLine == 0) then call writeln('newfile',sWithMe)
  87.  
  88. call close('oldfile')
  89. call close('newfile')
  90.  
  91. /* success! */
  92. say "File succesfully modified."
  93. exit(0)
  94.  
  95.  
  96. /* Changes all occurrences of the character '^' to spaces in the string. 
  97.    Needed to get around ARexx's lame argument parsing.   I think.
  98. */
  99. ChangeToSpaces: procedure
  100.     parse arg sOrig
  101.     
  102.     sNew = ""
  103.     do while (length(sOrig) > 0)
  104.         cChar = left(sOrig,1)
  105.         if (cChar == "^") then cChar = " "
  106.         sNew = sNew || cChar
  107.         sOrig = right(sOrig,length(sOrig)-1)
  108.         end
  109.     return sNew
  110.